<template>
  <a-layout class="app-layout">
    <a-layout :theme="getTheme" class="flex flex-col">
      <LayoutHeader :theme="getTheme" />
      <a-layout>
        <a-layout-sider
          :trigger="null"
          class="app-layout__sider"
          v-model:collapsed="layoutState.side.collapsed"
          collapsible
          :theme="getTheme"
          :width="layoutState.side.width"
          :collapsedWidth="layoutState.side.collapsedWidth"
        >
          <a-menu
            v-model:openKeys="openKeys"
            v-model:selectedKeys="selectedKeys"
            @select="handleMenuSelect"
            class="app-menu__wrapper"
            :theme="getTheme"
            mode="inline"
            @openChange="handleMenuChange"
            @click="handleMenuItemClick"
          >
            <component :is="whichComponent"> </component>
          </a-menu>
          <div class="side-trigger__wrapper">
            <menu-unfold-outlined v-if="layoutState.side.collapsed" class="trigger" @click="toggleCollapsed" />
            <menu-fold-outlined v-else class="trigger" @click="toggleCollapsed" />
          </div>
        </a-layout-sider>
        <a-layout>
          <LayoutContentTop class="layout__content-top" />
          <a-layout-content class="overflow-y-scroll w-[100%] bg-white">
            <router-view />
          </a-layout-content>
          <a-layout-footer class="bg-white"></a-layout-footer>
        </a-layout>
      </a-layout>
    </a-layout>
  </a-layout>
</template>

<script setup lang="ts">
import { computed, ref, watch, provide } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { layoutState, toggleCollapsed } from './state'
import LayoutHeader from './components/layout-header/LayoutHeader.vue'
import LayoutContentTop from './components/layout-content-top/LayoutContentTop.vue'
import { useThemeStore } from '@/stores/modules/projectConfig'
import useStore from '@/stores'
const { permission, system } = useStore()
const themeStore = useThemeStore()
console.log('system.imageDomain: ', system.imageDomain)
provide('imageDomain', system.imageDomain)
provide('baseUrl', import.meta.env.VITE_BASE_URL)
provide('localHost', window.location.origin + '/')

console.log(permission.siderType, 'siderType')
const route = useRoute()
const router = useRouter()
const openKeys = ref<string[]>()
const selectedKeys = ref<string[]>()

const siderType = computed(() => permission.siderType)
const whichComponent = computed(() =>
  siderType.value === 'ALL' ? 'CommonMenuList' : siderType.value === 'SITE' ? 'FilmMenuList' : 'AdMenuList'
)
const getTheme = computed(() => (themeStore.navTheme === 'light' ? 'light' : 'dark'))
watch(
  () => route.meta?.selectedMenu,
  () => {
    openKeys.value = [(route.meta?.selectedMenu ?? route.name) as string, route.meta?.parentMenu as string]
    console.log('openKeys: ', openKeys.value)
  },
  { immediate: true, deep: true }
)
watch(
  () => route.meta?.selectedMenu,
  () => {
    selectedKeys.value = [(route.meta?.selectedMenu ?? route.name) as string]
    console.log('selectedKeys: ', selectedKeys.value)
  },
  { immediate: true, deep: true }
)
const handleMenuSelect = (record: { key: string | string[] }) => {
  const key = record.key
  if (Array.isArray(key)) {
    if (key[1] === '_page') {
      window.open(key[0], '_blank')
      return
    }
    if (key[1] === '_blank') {
      window.open(router.resolve({ name: key[0] }).href, '_blank')
      return
    }
    return
  }
  router.replace({ name: key })
}
const handleMenuChange = (val: any) => {
  console.log('val: ', val)
  // openKeys.value = val
}
const handleMenuItemClick = (val: any) => {
  console.log('handleMenuItemClick: ', val, val.keyPath)
}
</script>
<style lang="less">
.ant-tooltip {
  max-width: 400px;
}
.ant-tooltip-inner {
  padding: 4px 8px;
  overflow: hidden;
  -webkit-line-clamp: 6;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  color: #5e5e5e !important;
}
.w-34 {
  width: 34px;
  display: inline-block;
  text-align: center;
}

.iconfont {
  margin-right: 10px;
}

.no-margin {
  margin: 0 0 0 8px;
}

:deep(.ant-layout-sider-collapsed) {
  .iconfont {
    width: 34px !important;
    display: inline;
  }
}

.ant-menu-submenu {
  .ant-menu-submenu-title {
    transition: color 0.4s;
    width: auto !important;
  }

  .ant-menu-sub {
    .ant-menu-item {
      .ant-menu-title-content {
        padding-left: 2.35rem !important;
      }
    }
  }
}

:deep(.ant-menu-submenu, .ant-menu-submenu-inline) {
  transition: border-color 0.4s, background 0.4s, padding 0.1s cubic-bezier(0.215, 0.61, 0.355, 1) !important;
}
.ant-menu-dark .ant-menu-sub,
.ant-menu.ant-menu-dark .ant-menu-sub {
  color: rgba(255, 255, 255, 0.65);
  background: rgb(43, 45, 53) !important;
  padding: 0px 12px;
  // box-sizing: border-box;
}
.ant-menu-sub {
  .ant-menu-item {
    .ant-menu-title-content {
      padding-left: 2.35rem !important;
    }
  }
}
.ant-menu-submenu-placement-rightTop > ul {
  padding: 10px 12px !important;
}
.ant-menu-submenu-placement-rightTop > ul > li {
  padding: 0px 2px !important;
  box-sizing: border-box;
}
.ant-menu-dark.ant-menu-dark:not(.ant-menu-horizontal) .ant-menu-item-selected {
  background-color: #202227;
}
#app {
  .app-layout {
    .ant-layout-sider-light {
      .ant-menu-light {
        background-color: #f5f5f5;
        .ant-menu-dark .ant-menu-sub,
        .ant-menu.ant-menu-dark .ant-menu-sub {
          color: rgba(255, 255, 255, 0.65);
          background: rgb(43, 45, 53) !important;
          padding: 0px 12px;
        }
        .ant-menu-item-selected {
          .ant-menu-title-content {
            background-color: #0083ff;
            color: #f5f5f5;
          }
        }
        .ant-menu-sub {
          .ant-menu-item {
            .ant-menu-title-content {
              padding-left: 3.1rem !important;
            }
          }
        }
      }
      .side-trigger__wrapper {
        background-color: #f5f5f5;
        .anticon {
          color: #000;
        }
      }
    }
  }
}

.ant-menu-inline-collapsed-tooltip {
  .ant-tooltip-inner {
    color: #fff !important;
    i {
      margin: 0;
    }
  }
}
.ant-menu-inline-collapsed .ant-menu-submenu .ant-menu-submenu-title .ant-menu-submenu-arrow {
  display: none !important;
}
.ant-table-tbody > tr > td {
  border-bottom: 0px solid #f0f0f0;
}
</style>
<style lang="less" scoped>
:deep(.ant-table-thead) {
  background-color: #fafafa;
}
:deep(.ant-table-thead > tr > th) {
  font-weight: 600;
  user-select: none;
}
.ant-menu {
  user-select: none;
}
.app-menu__wrapper {
  overflow-y: auto;
  overflow-x: clip;
}
.ant-layout {
  background-color: #fff;
}
:deep(.ant-btn) {
  border-radius: 3px;
}
.ant-layout-content {
  padding: 0 24px 17px;
  box-sizing: border-box;
  height: calc(100% - 101px);
}
.layout__content-top {
  padding-left: 24px;
  width: 100%;
  height: 56px;
  line-height: 55px;
  display: flex;
  align-items: center;
  font-size: 16px;
  color: #262626;
  letter-spacing: 1.28px;
  font-weight: 500;
  border-bottom: 1px solid #f1f1f1;

  .banner-tooltip {
    &-text {
      font-weight: 400;
      margin-left: 24px;
      font-size: 14px;
      color: #0083ff;
      display: flex;
      align-items: center;

      .iconfont {
        font-size: 18px;
        margin-right: 4px;
      }
    }
  }
}

:deep(.ant-pagination) {
  position: fixed;
  bottom: 20px;
  z-index: 2;
  right: 24px;
}

:deep(.ant-pagination-total-text) {
  line-height: 34px;
}
</style>
